package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName: CategoryServiceImpl
 * Package: com.sky.service.impl
 * Description:菜品分类的服务层实现类
 *
 * @Author: Zhang Peiyuan
 * @Create: 2024-12-18-10:57
 * @Version: v1.0
 */
@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {
    @Resource
    public CategoryMapper categoryMapper;
    @Resource
    public DishMapper dishMapper;
    @Resource
    public SetmealMapper setmealMapper;

    /**
     * 新增菜品分类
     * @param categoryDTO
     */
    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        // 将dto转化为entity
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setStatus(StatusConstant.DISABLE);
        // 插入数据库
        categoryMapper.sava(category);
    }

    /**
     * 分页查询菜品分类
     * @param type
     * @param name
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageResult queryCategoryPage(Integer type, String name, Integer page, Integer size) {
        // 1.启动分页查询
        PageHelper.startPage(page, size);
        // 2.查询数据
        Page<Category> result = categoryMapper.queryPage(type, name);
        // 3.封装结果
        return new PageResult(result.getTotal(), result.getResult());
    }

    /**
     * 更新菜品分类状态
     * @param status
     * @param id
     */
    @Override
    public void updateCategoryStatus(Integer status, Long id) {
        // 万精油更新
        Category build = Category.builder()
                .id(id)
                .status(status)
                .build();
        categoryMapper.update(build);
        return;
    }

    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        // 将dto转化为entity
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        // 插入数据库
        categoryMapper.update(category);
    }

    /**
     * 根据菜品的类型查询菜品分类, 无需分页
     * @return
     */
    @Override
    public List<Category> listCategory(Integer type) {
        return categoryMapper.selectListByType(type);
    }

    /**
     * 删除菜品/套餐分类
     * @param id
     */
    @Override
    public void deleteCategory(Long id) {
        // 判断当前菜品分类是否有dish被关联
        Integer count = dishMapper.slelctCountByCategoryId(id);
        if(count > 0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        // 判断当前套餐分类是否有套餐被关联
        count = setmealMapper.slelctCountByCategoryId(id);
        if(count > 0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        // 到这里就可以删除了
        categoryMapper.deleteById(id);
    }

}
